package org.knappsack.demo.seam.seamfaces.bean;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.knappsack.demo.seam.seamfaces.model.Teacher;
import org.knappsack.demo.seam.seamfaces.qualifier.DataRepository;

@SessionScoped
@FacesConverter("convert.teacher")
public class TeacherConverter implements Converter, Serializable {

	@Inject
	@DataRepository
	private EntityManager entityManager;

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		Long id = Long.valueOf(value);
		return entityManager.find(Teacher.class, id);
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		return ((Teacher) value).getId().toString();
	}

}
